package pers.mydb.connection.configuration;

import pers.mydb.throwable.MyDBException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 数据库连接配置文件
 */
public class Configuration {

    private Configuration() {
    }

    private Configuration(Configuration configuration) {
        this.dbUrl = configuration.dbUrl;
        this.dbUser = configuration.dbUser;
        this.dbPassword = configuration.dbPassword;
        this.dbDriverClass = configuration.dbDriverClass;
        this.modelUrls =configuration.modelUrls;
    }

    private String dbUrl,dbUser,dbPassword,dbDriverClass;// 数据库连接地址、用户名、密码、数据库驱动地址
    private List<String> modelUrls;// model列表

    public String getDbUrl() {
        return dbUrl;
    }

    public void setDbUrl(String dbUrl) {
        this.dbUrl = dbUrl;
    }

    public String getDbUser() {
        return dbUser;
    }

    public void setDbUser(String dbUser) {
        this.dbUser = dbUser;
    }

    public String getDbPassword() {
        return dbPassword;
    }

    public void setDbPassword(String dbPassword) {
        this.dbPassword = dbPassword;
    }

    public String getDbDriverClass() {
        return dbDriverClass;
    }

    public void setDbDriverClass(String dbDriverClass) {
        this.dbDriverClass = dbDriverClass;
    }

    public List<String> getModelUrls() {
        return modelUrls;
    }

    public void setModelUrls(List<String> modelUrls) {
        this.modelUrls = modelUrls;
    }

    //    /**
//     * 通过XML构建
//     */
//    public static class BuilderXML{
//        public static void main(String[] args) {
//            System.out.println(Thread.currentThread().getContextClassLoader().getResource("/").getPath());
//        }
//    }

    /**
     * 构建配置文件
     */
    public static class Builder{

        private Configuration configuration;

        public Builder() {
            configuration = new Configuration();
        }

        public Builder addDbUrl(String dbUrl){
            configuration.dbUrl = dbUrl;
            return this;
        }

        public Builder addDbUser(String dbUser){
            configuration.dbUser = dbUser;
            return this;
        }

        public Builder addDbPassword(String dbPassword){
            configuration.dbPassword = dbPassword;
            return this;
        }

        public Builder addDbDriverClass(String dbDriverClass){
            configuration.dbDriverClass = dbDriverClass;
            return this;
        }

        public Builder addModelUrls(String... modelUrls){
            configuration.modelUrls = Arrays.asList(modelUrls);
            return this;
        }

        public Configuration build() throws MyDBException {
            if(configuration.dbUrl == null){
                throw new MyDBException("数据库连接地址不能为空！");
            }
            if(configuration.dbDriverClass == null){
                throw new MyDBException("数据库驱动地址不能为空！");
            }
            if(configuration.dbUser == null){
                throw new MyDBException("数据库用户名不能为空！");
            }
            if(configuration.dbPassword == null){
                throw new MyDBException("数据库密码不能为空！");
            }
            return new Configuration(configuration);
        }
    }

}
